import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NavigatorUtil {
  /*
   * [context] 上下文对象
   * [targetPage] 目标页面
   * [isReplace]  是都替换挡墙页面
   */
  //普通打开页面的方法
  static void pushPage(
      {required BuildContext context,
      required Widget targetPage,
      bool isReplace = false,
      Function(dynamic value)? callBack}) {
    PageRoute pageRoute;
    if (Platform.isAndroid) {
      pageRoute = MaterialPageRoute(builder: (BuildContext context) {
        return targetPage;
      });
    } else {
      pageRoute = CupertinoPageRoute(builder: (BuildContext context) {
        return targetPage;
      });
    }
    if (isReplace) {
      Navigator.of(context).pushReplacement(pageRoute).then((value) {
        if (callBack != null) {
          callBack(value);
        }
      });
    } else {
      Navigator.of(context).push(pageRoute).then((value) {
        if (callBack != null) {
          callBack(value);
        }
      });
    }
  }

  /*
   * [context] 上下文对象
   * [targetPage] 目标页面
   * [isReplace]  是都替换挡墙页面
   */
  //普通打开页面的方法
  static void pushPageByFade(
      {required BuildContext context,
      required Widget targetPage,
      int startMills = 400,
      bool opaque = false,
      Function(dynamic value)? callBack}) {
    PageRoute pageRoute = PageRouteBuilder(
        opaque: opaque, //是否以背景透明的方式打开
        transitionDuration: Duration(milliseconds: startMills),
        //构建过度动画
        transitionsBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation, Widget child) {
          return FadeTransition(
            opacity: animation,
            child: child,
          );
        },
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secodaryAnimation) {
          return targetPage;
        });
    Navigator.of(context).push(pageRoute).then((value) {
      if (callBack != null) {
        callBack(value);
      }
    });
  }
}
